package telepasea.app;

import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView; 

public class ListFilterUI extends Activity {
	Button button1;
	private static String LF_M;
	private static String LF_F;
	private static String LF_ACT1;
	private static String LF_ACT2;
	private static String LF_ACT3;
	private static String LF_ACT4;
	private static String LF_ACT5;
	CheckBox checkbox1;//male
	CheckBox checkbox2;//female
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listfilter);

		ListView itemlist = (ListView) findViewById(R.id.listView4);
		// ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
		// this, R.array.ActivityList,
		// android.R.layout.simple_list_item_multiple_choice);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.ActivityList, R.layout.list_multiple_choice);
		itemlist.setAdapter(adapter);
		itemlist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
		readListFilter();
	
		
		button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				
				writeListFilter();
				TelepaSEAActivity.update = 1;
				finish();
			}
		});
	}
	
	public void readListFilter(){
		checkbox1 = (CheckBox) findViewById(R.id.checkBox3);
		checkbox2 = (CheckBox) findViewById(R.id.checkBox4);
		
		ListView itemlist = (ListView) findViewById(R.id.listView4);

		LF_M = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "LF_M");
		LF_F = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "LF_F");
		LF_ACT1 = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "LF_ACT1");
		LF_ACT2 = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "LF_ACT2");
		LF_ACT3 = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "LF_ACT3");
		LF_ACT4 = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "LF_ACT4");
		LF_ACT5 = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "LF_ACT5");

		if (LF_M.equals("checked"))
			checkbox1.setChecked(true);
		if (LF_F.equals("checked"))
			checkbox2.setChecked(true);
		if (LF_ACT1.equals("checked"))
			itemlist.setItemChecked(0, true);
		if (LF_ACT2.equals("checked"))
			itemlist.setItemChecked(1, true);
		if (LF_ACT3.equals("checked"))
			itemlist.setItemChecked(2, true);
		if (LF_ACT4.equals("checked"))
			itemlist.setItemChecked(3, true);
		if (LF_ACT5.equals("checked"))
			itemlist.setItemChecked(4, true);
	}
	
	public void writeListFilter(){
		ListView itemlist = (ListView) findViewById(R.id.listView4);
		if (checkbox1.isChecked())
			Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "LF_M", "checked");
		else
			Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "LF_M", "");
		
		if (checkbox2.isChecked())
			Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "LF_F", "checked");
		else
			Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "LF_F", "");
		
		SparseBooleanArray sb = itemlist.getCheckedItemPositions();
		
		if (sb.size() != 0) {
			for(int i = itemlist.getCount()-1; i >=0 ; i--) {
				int j = i + 1;
				
				if(sb.get(i))		
					Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "LF_ACT"+j, "checked");
				else
					Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "LF_ACT"+j, "");
				
			}			
		}
	}
}
